package com.codi.bus.core.fund.manager;

import com.codi.base.util.EqualsUtil;
import com.codi.bus.constant.FundConst;
import com.codi.bus.core.fund.dao.FundDividendChangeDao;
import com.codi.bus.core.fund.domain.FundDividendChange;
import com.codi.bus.core.fund.dto.FundDividendChangeQueryDto;
import com.codi.bus.core.fund.enums.DividendAutoBuyEnum;
import com.codi.bus.core.fund.enums.FundDividendChangeBizTypeEnum;
import com.codi.bus.core.fund.enums.FundDividendChangeStatusEnum;
import com.codi.bus.core.service.FundCombinationService;
import com.codi.bus.core.service.FundDetailService;
import com.codi.bus.core.service.QueryService;
import com.codi.bus.core.service.model.PortfolioQryModel;
import com.codi.bus.core.service.model.ShareTypeModel;
import com.codi.fundData.domain.FundDetail;
import com.github.pagehelper.PageHelper;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 模块名
 *
 * @author spy
 * @version 1.0 2018-01-29
 * @since 1.0
 */
@Slf4j
@Component
public class FundDividendChangeManager {

    @Autowired
    private FundDividendChangeDao fundDividendChangeDao;

    @Autowired
    private FundCombinationService fundCombinationService;

    @Autowired
    private QueryService queryService;

    @Autowired
    private FundDetailService fundDetailService;


    public void add(FundDividendChange dto) {
        Preconditions.checkNotNull(dto);

        dto.setCreateDate(new Date());


        fundDividendChangeDao.insertSelective(dto);
    }

    public void addPortfolioApplyWaitAsync(FundDividendChange dto) {
        Preconditions.checkNotNull(dto);
        Preconditions.checkNotNull(dto.getPortfolioCode());


        // 判断用户有没有购买过该组合

        Long portfolioCount = PageHelper.count(() -> {

            FundDividendChangeQueryDto queryDto = FundDividendChangeQueryDto.builder()
                                                                            .userId(dto.getUserId())
                                                                            .portfolioCode(dto.getPortfolioCode())
                                                                            .innerPortfolioCode(dto.getInnerPortfolioCode())
                                                                            .build();
            fundDividendChangeDao.select(queryDto);
        });

        if (portfolioCount > 0) {
            log.info("用户[{}]已购买过该组合portfolioCode={},innerPortfolioCode={}", dto.getUserId(),
                dto.getPortfolioCode(), dto.getInnerPortfolioCode());
            return;
        }


        //FIXME 假设用户没有购买过，那就需要进行申请分红修改

        // 查询该组合中的基金
        List<PortfolioQryModel> portfolios = fundCombinationService.queryPorfolio(dto.getPortfolioCode());


        dto.setCreateDate(new Date());
        dto.setBizType(FundDividendChangeBizTypeEnum.PORTFOLIO.getKey());
        dto.setStatus(FundDividendChangeStatusEnum.WAIT_APPLY.getKey());
        dto.setAutoBuy(DividendAutoBuyEnum.REINVESTMENT.getKey());

        for (int i = 0; i < portfolios.size(); i++) {
            PortfolioQryModel portfolioQryModel = portfolios.get(i);
            // concurrency
            FundDetail fundDetail = fundDetailService.getFundDetail(portfolioQryModel.getFundCode());

            if (fundDetail == null) {
                log.warn("基金编码不存在{}", portfolioQryModel.getFundCode());
                continue;
            }

            if (EqualsUtil.equals(FundConst.FUND_TYPE_CODE_CURRENCY, fundDetail.getFundTypeCode().intValue())) {
                log.info("货币基金无需修改分红方式");
                continue;
            }

            log.debug("【修改组合分红】添加组合【{},innerPortfolioCode={}】中的基金【{}】",
                portfolioQryModel.getPortfolioCode(),
                portfolioQryModel.getInnerCombcode(),
                portfolioQryModel.getFundCode());

            // share type
            ShareTypeModel shareTypeModel = queryService.getShareType(dto.getFundCode());

            if (shareTypeModel == null) {
                log.error("fail to query share type");
            } else {
                dto.setShareType(shareTypeModel.getShareType());
                dto.setFundCode(portfolioQryModel.getFundCode());
                fundDividendChangeDao.insertSelective(dto);

                log.info("插入待执行的脚本");
            }
        }


    }

}
